package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.story.common.annotion.BussinessLog;
import com.story.common.annotion.Permission;
import com.story.common.constant.Dict;
import com.story.common.constant.factory.PageFactory;
import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.dao.FeedbackMapper;
import com.story.common.persistence.model.Feedback;
import com.story.core.base.controller.BaseController;
import com.story.core.util.ToolUtil;
import com.story.modular.app.dao.FeedbackDao;
import com.story.modular.app.warpper.FeedbackWarpper;
import com.story.modular.app.warpper.VedioWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @decription
 * @Date 2017-12-29 8:42
 */
@Controller
@RequestMapping(value = "/feedback")
public class FeedbackController extends BaseController{

    public static final String PREFIX = "/app/feedback/";

    @Autowired
    private FeedbackDao feedbackDao;

    @Autowired
    private FeedbackMapper feedbackMapper;

    /**
     * 进入反馈处理首页
     * @return
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "feedback.html";
    }

    /**
     * 得到列表
     * @param qqNumber
     * @param status
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "qqNumber",required = false) String qqNumber,
                       @RequestParam(value = "status",required = false) Boolean status,
                       @RequestParam(value = "beginTime",required = false) String startTime,
                       @RequestParam(value = "endTime",required = false) String endTime) {
        Page<Feedback> page = new PageFactory<Feedback>().defaultPage();
        List<Map<String,Object>> feedbackList = feedbackDao.getFeedbackList(page, qqNumber.trim(),status,startTime,endTime,page.getOrderByField(), page.isAsc());
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        for (Map<String, Object> map : feedbackList) {
            Map<String,Object> resultMap = new HashMap<String,Object>();
            resultMap.put("id",map.get("id"));
            resultMap.put("qqNumber",map.get("qqNumber"));
            resultMap.put("optionContent",map.get("optionContent"));
            if((Boolean) map.get("status")) {
                resultMap.put("status","已处理");
            } else {
                resultMap.put("status","未处理");
            }
            resultMap.put("createTime",map.get("createTime"));
            resultList.add(resultMap);
        }
        page.setRecords((List<Feedback>) new FeedbackWarpper(resultList).warp());
        return super.packForBT(page);
    }

    /**
     * 进入修改状态页面
     * @param feedbackId
     * @param model
     * @return
     */
    @RequestMapping(value = "/feedbackUpdate/{feedbackId}")
    public String toUpdate(@PathVariable(value = "feedbackId") Integer feedbackId, Model model) {
        Feedback feedback = feedbackDao.getFeedbackById(feedbackId);
        model.addAttribute("feedback",feedback);
        return PREFIX + "feedback_handle.html";
    }

    /**
     * 改变处理状态
     * @param feedback
     * @return
     */
    @BussinessLog(value = "改变反馈处理状态", key = "name", dict = Dict.FeedbackDict)
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public Object update(Feedback feedback) {
        if(ToolUtil.isOneEmpty(feedback,feedback.getId())) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        feedbackDao.updateStatusById(feedback.getId());
        return super.SUCCESS_TIP;
    }

    /**
     * 删除反馈信息
     * @param id
     * @return
     */
    @BussinessLog(value = "删除反馈信息", key = "id", dict = Dict.FeedbackDict)
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public Object delete(@RequestParam(value = "feedbackId") Integer id) {
        feedbackMapper.deleteById(id);
        return super.SUCCESS_TIP;
    }
}
